package com.ting.test.algorithms.stack;

/**
 * 利用数组 自定义一个
 * 队列
 * 先进先出->头部进 尾部出
 */
public class 自定义队列 {

    public static void main(String[] args) {
        int[] arr = new int[]{11, 2, 4, 6, 8, 9, 0, 6};
        MyQueue stack = new MyQueue(7);
        stack.get();
        for (int s : arr) {
            boolean push = stack.add(s);
            System.out.println("push:" + s + "," + push);
        }

        for (int s : arr) {
            Integer push = stack.get();
            System.out.println(push);
        }
    }

    static class MyQueue {
        int[] queue;
        int length;
        int start;
        int end;
        int size = 0;

        public MyQueue(int size) {
            queue = new int[size];
            length = size;
        }

        public Integer get() {
            if (size <= 0) {
                return null;
            }
            size--;
            end = getIndex(end);
            return queue[end++];
        }

        public int getIndex(int param) {
            return  Math.abs(param % length);
        }

        public boolean add(int param) {
            if (size >= queue.length) {
                return false;
            }
            size++;
            start = getIndex(start);
            queue[start++] = param;
            return true;
        }
    }
}
